<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ page import="awesome.Manager.UserManager" %>
    <%@ page import="awesome.Manager.User" %>
    
    
<%
	String username = "";
	String password = "";
	
	username = request.getParameter("username");
	password = request.getParameter("password");
	
	if(username!=null && password!=null && username!="" && password!="")
	{
	UserManager um = new UserManager();
	boolean loggedIn = um.login(username, password);
	
		if(loggedIn)
		{
			User user = um.getUser(username);

			System.out.println(user.getId());
			System.out.println(user.getFirstName());
			session.setAttribute("loggedIn", true);
			session.setAttribute("username", username);
			session.setAttribute("uid", user.getId());
			session.setAttribute("firstname", user.getFirstName());
			
			response.sendRedirect("index.jsp");
		}
		else
		{
		%>
		
			<%@include file='templates/header.jsp'%>
			<div style="color:#c51e3a">Invalid username and password combination!</div> <br />
			
			<form action="login.jsp" method="post">
				<table>
				<tr>
				<td>Username:</td>
				<td><input type="text" name = "uid" />
			 	<span style="font-size:11px; font-weight:100"> ex. rob</span></td>
				</tr>
				<tr>
				<td>Password:</td>
				<td><input type="password" name = "password" /></td>
				</tr>
				<tr>
					<td></td>
					<td><input type="submit" value="submit" /></td>
				</tr>
				</table>
			</form>
			<%@include file='templates/footer.jsp'%>

		<% 	
		}
	}
	else
	{
%>
<%@include file='templates/header.jsp'%>
<% 
	String errorID= request.getParameter("errorID");
	if(errorID!=null && errorID.equals("1"))	{ %>
	<div style="color:#c51e3a">You are not logged in. </div> 
		 Please specify your username and password to Login.<br /> <br />
	<%
	}
	%>
	
	<h3>Login </h3> <br />
	<form action="login.jsp" method="post">
	<table>
		<tr>
			<td>Username:</td>
			<td><input type="text" name = "username" />
			 <span style="font-size:11px; font-weight:100"> ex. rob</span></td>
		</tr>
		<tr>
			<td>Password:</td>
			<td><input type="password" name = "password" /></td>
		</tr>
		<tr>
			<td></td>
			<td><input type="submit" value="submit" /></td>
		</tr>
	</table>
	</form>
	<br />
	<h2> Dont have an account? <a href="signup.jsp">Sign up</a> Now.</h2>
<%@include file='templates/footer.jsp'%>
<%
}
%>